import { Task } from "./app_base/module/task/Task";
import { TaskQueue } from "./app_base/module/task/TaskQueue";
import { viewAnalytics } from "./app_base/module/view/ViewManager";
import { viewClass } from "./app_cocos/View/CCPViewLoader";
import { Scene } from "./app_cocos/View/Scene";
import { app } from "./base";
import { BootControl } from "./ui/boot/BootControl";
import { StartView } from "./ui/view/StartView";


const { ccclass, property } = cc._decorator;

@ccclass
@viewAnalytics("启动场景")
@viewClass({
    name: "Boot",
    path: ""
})

export class BootScene extends Scene {
    @property(cc.String)
    private nextScenePath: string = "MainRes://view/Main";

    @property(cc.ProgressBar)
    private progressBar: cc.ProgressBar = null

    _task: TaskQueue = new TaskQueue();
    public setData(): void {
        let control: BootControl = new BootControl();
        this.setControl(control);
        let label: string = "[初始化完成]";
        console.time(label);

        //加载服务器的配置
        // control.init().then(() => {


        // }).catch((reason) => {

        // });

        // app.res.load("MainRes://Prefab/StartView", cc.Prefab, this._task)
        // console.timeEnd(label);
        // this._task.once(Task.Event.COMPLETE, this, this._onComplete);
        // this._task.on(Task.Event.ERROR, this, this._onError);
        // this._task.on(Task.Event.PROGRESS, this, this._onProgress);
        // control.load(this.nextScenePath);
        this._control.switchView(StartView).then(() => {
            let loadingNode = this.getChildByPath("loading")
            loadingNode && loadingNode.destroy()
        })
    }

    private async _onComplete() {
        console.log(`[预加载-完成]`);

        //登录
        // let success = await this.control<BootControl>().login().catch((reason) => {
        //     console.log(`[登录-失败]:${reason}`);
        // });
        // if (success) {
        console.log(`[登录-成功]`);
        // this.control().offAllEvent();
        // this.control<BootControl>().destroy();
        // this._control = null;
       
        // }
    }

    private _onError(task: Task, error: { code: string, msg: string; }): void {
        // this._control.offAllEvent();
        console.error(`[预加载-错误] 任务:${error.code}  错误信息:${error.msg}`);

    }

    private _onProgress(event: string, task: Task, percent: number): void {
        console.log(`[预加载-进度] ${event} ${(percent * 100).toFixed(1)}%`);
        // todo 更新进度条
        // percent = percent - this.progressBar.progress;
        // if (percent > 0) {
        //     cc.Tween.stopAllByTarget(this.progressBar);
        //     cc.tween(this.progressBar).by(percent, { progress: percent }).call(() => {
        //         if (this.progressBar.progress >= 1) {
        //             // cc.director.loadScene(app.res.parsePath(this.nextScenePath).path);
        //         }
        //     }).start();
        // }
    }

    protected onDestroy(): void {
        this.progressBar = null;
        this.bundle = null;
        this._control = null;
    }
}